package com.cg.controller;

import com.cg.entity.Category;
import com.cg.entity.Events;
import com.cg.entity.User;
import com.cg.service.CategoryService;
import com.cg.service.EventsService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class EventsController {
    @Autowired
    private EventsService eventsService;
    @Autowired
    private CategoryService categoryService;
    @RequestMapping(value = "/event")
    public String event(HttpServletRequest request,Model model){
        User user = (User) request.getSession().getAttribute("user");
        if(user!=null){
            Integer userId= user.getUserId();
            List<Events> eventsList= eventsService.eventsList(userId,null,"",null,null);
            request.getSession().setAttribute("eventsList", eventsList);
            List<Category>categoryList= categoryService.categoryList(userId,"");
            request.getSession().setAttribute("categoryList", categoryList);
            return "event";
        }else{
            model.addAttribute("MainError", "请先登录");
            return "login";
        }

    }
    @RequestMapping(value = "/doAddEvent")
    public String addevent(HttpServletRequest request, @RequestParam("title") String title,
                           @RequestParam("categoryId") Integer categoryId,
                           @RequestParam("startDate") String startDate,
                           @RequestParam("priority") Integer priority,
                           @RequestParam("endDate") String endDate,
                           @RequestParam("status") Integer status,
                           @RequestParam("description") String description, Model model
                           ){
         User user = (User) request.getSession().getAttribute("user");
         Integer userId = user.getUserId();
         int one = eventsService.findByTitle(title,userId);
        if (one==0){
            eventsService.addevent(userId,title,categoryId,startDate,priority,endDate,status,description);
            return "redirect:/user/event";
        }else{
            model.addAttribute("eventsError", "该事件已存在");
            return "event";
        }
    }
    @RequestMapping(value = "/addevent")
    public String addevent(){
        return "addEvent";
    }

    @RequestMapping(value = "/editEvent")
    public String editevent(@RequestParam("eventId") Integer eventId,HttpServletRequest request) {
        Events event = eventsService.findEventById(eventId);
        request.getSession().setAttribute("event", event);
        return "editEvent";
    }
    @RequestMapping(value = "/doEditEvent")
    public String editevent(HttpServletRequest request,@RequestParam("title") String title,
                           @RequestParam("categoryId") Integer categoryId,
                           @RequestParam("startDate") String startDate,
                           @RequestParam("priority") Integer priority,
                           @RequestParam("endDate") String endDate,
                           @RequestParam("status") Integer status,
                           @RequestParam("description") String description, @RequestParam("eventId") Integer eventId,Model
model){
        User user = (User) request.getSession().getAttribute("user");
        Integer userId = user.getUserId();
        int one = eventsService.findByTitle(title,userId);
        if (one<=1){
            eventsService.editEvent(title,categoryId,startDate,priority,endDate,status,description,eventId);
            return "redirect:/user/event";
        }else{
            model.addAttribute("eventsError", "该事件已存在");
            return "event";
        }
    }
    @RequestMapping(value = "/deleteEvent")
    public String deleteEvent(@RequestParam("eventId") Integer eventId) {
        eventsService.deleteEvent(eventId);
        // 重定向到分类列表页面
        return "redirect:/user/event";
    }
    @RequestMapping("/searchEvents")
    public String searchEvents(@RequestParam(value = "categoryId", required = false) Integer categoryId,
                               @RequestParam(value = "title", required = false) String title,
                               @RequestParam(value = "priority", required = false) Integer priority,
                               @RequestParam(value = "status", required = false) Integer status, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        Integer userId = (user != null) ? user.getUserId() : 0;
        List<Events> eventsList = eventsService.eventsList(userId, categoryId, title, priority, status);
        request.getSession().setAttribute("eventsList", eventsList);
        return "event";
    }

    @RequestMapping(value = "/detailEvent")
    public String detailevent(@RequestParam("eventId") Integer eventId,HttpServletRequest request) {
        Events event = eventsService.findEventById(eventId);
        request.getSession().setAttribute("event", event);
        return "detailEvent";
    }
}
